package io.aicn.lowcode.controller;

import io.aicn.lowcode.dto.ProductCreate;
import io.aicn.lowcode.dto.ProductUpdate;
import io.aicn.lowcode.entity.Product;
import io.aicn.lowcode.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/products")
@Tag(name = "Product Management", description = "Product CRUD operations")
public class ProductController {
    
    @Autowired
    private IProductService productService;
    
    @GetMapping
    @Operation(summary = "Get all products", description = "Retrieve a list of all products in the system")
    public ResponseEntity<List<Product>> getAllProducts() {
        List<Product> products = productService.getAllProducts();
        return ResponseEntity.ok(products);
    }
    
    @PostMapping
    @Operation(summary = "Create a new product", description = "Add a new product to the system")
    public ResponseEntity<Product> createProduct(@Valid @RequestBody ProductCreate productCreate) {
        Product product = productService.createProduct(productCreate);
        return ResponseEntity.status(HttpStatus.CREATED).body(product);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "Get product by ID", description = "Retrieve a specific product by their ID")
    public ResponseEntity<Product> getProductById(
            @Parameter(description = "Product ID") @PathVariable Long id) {
        Product product = productService.getById(id);
        if (product == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(product);
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "Update product", description = "Update an existing product's information")
    public ResponseEntity<Product> updateProduct(
            @Parameter(description = "Product ID") @PathVariable Long id,
            @Valid @RequestBody ProductUpdate productUpdate) {
        Product product = productService.updateProduct(id, productUpdate);
        if (product == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(product);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "Delete product", description = "Delete a product from the system")
    public ResponseEntity<Void> deleteProduct(
            @Parameter(description = "Product ID") @PathVariable Long id) {
        boolean deleted = productService.deleteProduct(id);
        if (!deleted) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.noContent().build();
    }
    
    @GetMapping("/search")
    @Operation(summary = "Search products", description = "Search products by various criteria")
    public ResponseEntity<List<Product>> searchProducts(
            @Parameter(description = "Product name (partial match)") @RequestParam(required = false) String name,
            @Parameter(description = "Product code (partial match)") @RequestParam(required = false) String code,
            @Parameter(description = "Product category (partial match)") @RequestParam(required = false) String category,
            @Parameter(description = "Minimum price") @RequestParam(required = false) BigDecimal minPrice,
            @Parameter(description = "Maximum price") @RequestParam(required = false) BigDecimal maxPrice,
            @Parameter(description = "Minimum stock") @RequestParam(required = false) Integer minStock,
            @Parameter(description = "Maximum stock") @RequestParam(required = false) Integer maxStock,
            @Parameter(description = "Product status") @RequestParam(required = false) String status) {
        List<Product> result = productService.searchProducts(name, code, category, minPrice, maxPrice, minStock, maxStock, status);
        return ResponseEntity.ok(result);
    }
}